package org.jeecg.activiti.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.activiti.engine.HistoryService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.task.Task;
import org.jeecg.activiti.entity.ActKAppendFormData;
import org.jeecg.activiti.entity.ActKAppendFormDeployment;
import org.jeecg.activiti.entity.ActKNode;
import org.jeecg.activiti.entity.ActReModelFormData;
import org.jeecg.activiti.mapper.ActReModelFormDataMapper;
import org.jeecg.activiti.service.*;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO 表单数据service
 *
 * @author dousw
 * @version 1.0
 * @date 2020/9/15 11:00
 */

@Service
public class ActReModelFormDataServiceImpl extends ServiceImpl<ActReModelFormDataMapper, ActReModelFormData> implements IActReModelFormDataService {

    @Autowired
    private ActReModelFormDataMapper actReModelFormDataMapper;

    @Autowired
    private TaskService taskService;

    @Autowired
    private HistoryService historyService;

    @Autowired
    private IActKAppendFormDeploymentService iActKAppendFormDeploymentService;

    @Autowired
    private IActKAppendFormDataService iActKAppendFormDataService;

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private IActKNodeService iActKNodeService;

    @Override
    public Page<ActReModelFormData> pageList(Page<ActReModelFormData> page, ActReModelFormData actReModelFormData) {
        List<ActReModelFormData> actReModelFormDataList = actReModelFormDataMapper.pageList(page, actReModelFormData);
        page.setRecords(actReModelFormDataList);
        return page;
    }

    @Override
    public List<Map<String, Object>> handleNodeAppendForm(String taskId, String processInstanceId) {
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        ActKNode actKNode = iActKNodeService.getOne(new QueryWrapper<ActKNode>()
                .eq("node_id", task.getTaskDefinitionKey())
                .eq("process_definition_id", task.getProcessDefinitionId()));

        List<Map<String, Object>> result = new ArrayList<>();

        if (actKNode.getIsShowAppendForm()) {
            List<HistoricActivityInstance> historicActivityInstances = historyService.createHistoricActivityInstanceQuery()
                    .processInstanceId(processInstanceId).finished().orderByHistoricActivityInstanceEndTime().asc().list();
            for (HistoricActivityInstance historicActivityInstance : historicActivityInstances) {
                Map<String, Object> tempMap = new HashMap<>();

                String activityId = historicActivityInstance.getActivityId();
                ActKAppendFormDeployment actKAppendFormDeployment = iActKAppendFormDeploymentService.getOne(new QueryWrapper<ActKAppendFormDeployment>()
                        .eq("node_id", activityId).eq("process_definition_id", task.getProcessDefinitionId()));
                if (actKAppendFormDeployment != null) {
                    tempMap.put("nodeId", activityId);
                    tempMap.put("formJson", actKAppendFormDeployment.getFormJson());
                    tempMap.put("nodeName", historicActivityInstance.getActivityName());
                    tempMap.put("assignee", iSysUserService.getUserByName(historicActivityInstance.getAssignee()).getRealname());

                    ActKAppendFormData actKAppendFormData = iActKAppendFormDataService.getOne(new QueryWrapper<ActKAppendFormData>().eq("node_id", activityId)
                            .eq("process_instance_id", processInstanceId));

                    if (actKAppendFormData != null) {
                        tempMap.put("formData", actKAppendFormData.getFormData());
                    }

                    result.add(tempMap);
                }
            }
        }
        return result;
    }
}
